/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.wx.service;

import java.util.*;

import com.alibaba.fastjson.JSONArray;
import com.jeesite.common.lang.DateUtils;
import com.jeesite.modules.common.refun.CollectionUtil;
import com.jeesite.modules.common.refun.StringUtil;
import com.jeesite.modules.wx.entity.*;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.wx.dao.WxUserActivityAfterDao;

/**
 * wx_user_activity_afterService
 * @author wangjianxin
 * @version 2018-10-03
 */
@Service
@Transactional(readOnly=true)
public class WxUserActivityAfterService extends CrudService<WxUserActivityAfterDao, WxUserActivityAfter> {

	@Autowired
	private WxUserActivityAfterDao wxUserActivityAfterDao;
	@Autowired
	private WxUserActivityAfterService wxUserActivityAfterService;
	@Autowired
    private WxUserService wxUserService;
	@Autowired
	private WxUserDisValueRecordService wxUserDisValueRecordService;
	@Autowired
	private WxBusinessGroupService wxBusinessGroupService;

	/**
	 * 获取单条数据
	 * @param wxUserActivityAfter
	 * @return
	 */
	@Override
	public WxUserActivityAfter get(WxUserActivityAfter wxUserActivityAfter) {
		return super.get(wxUserActivityAfter);
	}
	
	/**
	 * 查询分页数据
	 * @param page 分页对象
	 * @param wxUserActivityAfter
	 * @return
	 */
	@Override
	public Page<WxUserActivityAfter> findPage(Page<WxUserActivityAfter> page, WxUserActivityAfter wxUserActivityAfter) {
		return super.findPage(page, wxUserActivityAfter);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param wxUserActivityAfter
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(WxUserActivityAfter wxUserActivityAfter) {
		super.save(wxUserActivityAfter);
	}
	
	/**
	 * 更新状态
	 * @param wxUserActivityAfter
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(WxUserActivityAfter wxUserActivityAfter) {
		super.updateStatus(wxUserActivityAfter);
	}
	
	/**
	 * 删除数据
	 * @param wxUserActivityAfter
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(WxUserActivityAfter wxUserActivityAfter) {
		super.delete(wxUserActivityAfter);
	}

	/**
	 * 统计商家核销总数
	 * @param businessId
	 * @param type
	 * @return
	 */
	public Integer afterCount(String businessId,String type,String activityId){
		Map map = new HashMap();
		map.put("businessId",businessId);
		map.put("activityType",type);
		map.put("currentDate", new Date());
		map.put("activityId",activityId );
		return wxUserActivityAfterDao.afterCount(map);
	}

	/**
	 * 兼容（使用中）
	 * @param wxActivity
	 * @return
	 */
	public Map<String,String> mergeOrder(WxActivity wxActivity){

		Map<String,String> map = new HashMap();
		//获得活动的商家分组
		Long groupId = wxActivity.getBusinessGroupId();
		//通过分组查询分组里面的商家
		WxBusinessGroup wxBusinessGroup = new WxBusinessGroup();
		wxBusinessGroup.setConfigGroupId(groupId);
		List<WxBusinessGroup> wxBusinessList = wxBusinessGroupService.findList(wxBusinessGroup);
		if(CollectionUtil.isNotEmpty(wxBusinessList)){
			WxBusinessGroup findBusinessGroup = wxBusinessList.get(0);
			//获取分组的商家ids
			String businessIds = findBusinessGroup.getBusinessIds();
			if(StringUtil.isNotEmpty(businessIds)){
				String[] businessIdsArray = businessIds.split(",");
				int len = businessIdsArray.length;
				for(int i = 0; i < len; i++){
					String businessId = businessIdsArray[i];
					map.put(businessId,businessId);
				}
			}

		}

		return map;
	}



	/**
	 *（不用）
	 * @param wxActivity
	 * @param activityId
	 * @param userOpenId
	 */
    @Transactional(readOnly=false,rollbackFor = Exception.class)
    public void jianrong(WxActivity wxActivity,String activityId, String userOpenId) {
		//获得活动的商家
		String business = wxActivity.getBusiness();
		//活动的所有商家list
		List<String> allActivityBusinessList = new LinkedList();
		if (StringUtil.isNotEmpty(business)) {
			String[] businessArray = business.split(",");
			int len = businessArray.length;
			for (int i = 0; i < len; i++) {
				//得到商家id
				String businessId = businessArray[i];
				allActivityBusinessList.add(businessId);
			}
		}

		//先去查找是否购买了
		WxUserDisValueRecord wxUserDisValueRecord = new WxUserDisValueRecord();
		wxUserDisValueRecord.setBuyUserOpenId(userOpenId);
		wxUserDisValueRecord.setBuyActivityId(Long.valueOf(activityId));
		List<WxUserDisValueRecord> wxUserDisValueRecordList = wxUserDisValueRecordService.findList(wxUserDisValueRecord);

		if(CollectionUtil.isNotEmpty(wxUserDisValueRecordList)){

			WxUserActivityAfter wxUserActivityAfter = new WxUserActivityAfter();
			wxUserActivityAfter.setUserOpenId(userOpenId);
			wxUserActivityAfter.setActivityId(activityId);

			List<WxUserActivityAfter> wxUserActivityAfterList = wxUserActivityAfterService.findList(wxUserActivityAfter);
			if (CollectionUtils.isNotEmpty(wxUserActivityAfterList)) {

				Map<String, List<WxUserActivityAfter>> map = new HashMap();

				for (WxUserActivityAfter te : wxUserActivityAfterList) {

					//查询用户的核销商家，和活动的商家进行比对，需要添加活动后续添加的商家
					String businessId = te.getBusinessId();
					if (allActivityBusinessList.contains(businessId)) {
						//删除只剩下 核销里面没有查询出来的
						allActivityBusinessList.remove(businessId);
					}
				}

				if(CollectionUtil.isNotEmpty(allActivityBusinessList)){

					for(String b : allActivityBusinessList){
						WxUser wxUser = wxUserService.findByOpenId(userOpenId);
						WxUserActivityAfter newAddActivityAfter = new WxUserActivityAfter();
						//订单号
						newAddActivityAfter.setOrderNum(wxActivity.getId()+wxUserActivityAfterList.size()+1);
						newAddActivityAfter.setActivityId(activityId);
						newAddActivityAfter.setBusinessId(b);
						newAddActivityAfter.setUserOpenId(userOpenId);
						newAddActivityAfter.setUserId(wxUser.getId());
						wxUserActivityAfterService.insert(newAddActivityAfter);
					}

				}
			}
		}

    }
}